package com.shanzhu.parking.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shanzhu.parking.common.R;
import com.shanzhu.parking.entity.po.FeeSetting;
import com.shanzhu.parking.service.FeeSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 车位费用设置表 前端控制器
 * </p>
 *
 * @author 只茂瑞
 * @since 2025-04-13
 */
@RestController
@RequestMapping("/fee-setting")
@RequiredArgsConstructor
@Api(tags = "费率设置管理")
public class FeeSettingController {

    private final FeeSettingService feeSettingService;
    
    /**
     * 获取费用设置列表
     *
     * @param carType 车辆类型
     * @return 费用设置列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取费率设置列表", notes = "根据车辆类型获取费率设置列表")
    public R<List<FeeSetting>> list(String carType) {
        LambdaQueryWrapper<FeeSetting> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(carType)) {
            queryWrapper.like(FeeSetting::getCarType, carType);
        }
        queryWrapper.orderByAsc(FeeSetting::getCarType).orderByAsc(FeeSetting::getStartTime);
        
        List<FeeSetting> feeList = feeSettingService.list(queryWrapper);
        return R.success(feeList);
    }
    
    /**
     * 添加费用设置
     *
     * @param feeSetting 费用设置信息
     * @return 添加结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加费率设置", notes = "添加新的费率设置信息")
    public R<Boolean> add(@RequestBody FeeSetting feeSetting) {
        // 设置创建时间
        feeSetting.setFeeTime(LocalDateTime.now());
        
        boolean result = feeSettingService.save(feeSetting);
        return R.success(result);
    }
    
    /**
     * 更新费用设置
     *
     * @param feeSetting 费用设置信息
     * @return 更新结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新费率设置", notes = "更新已有费率设置信息")
    public R<Boolean> update(@RequestBody FeeSetting feeSetting) {
        boolean result = feeSettingService.updateById(feeSetting);
        return R.success(result);
    }
    
    /**
     * 删除费用设置
     *
     * @param id 费用设置ID
     * @return 删除结果
     */
    @GetMapping("/delete")
    @ApiOperation(value = "删除费率设置", notes = "根据ID删除费率设置")
    public R<Boolean> delete(Integer id) {
        boolean result = feeSettingService.removeById(id);
        return R.success(result);
    }
}
